package ${package.Service}.basic;

import ${package.Entity}.${table.entityName};
import ${cfg.parent}.${package.ModuleName}.query.${table.entityName}Query;
import ${cfg.parent}.${package.ModuleName}.wrapper.${table.entityName}Wrapper;
import com.justgame.project.base.servcice.IBasicService;
<#assign imports=cfg.tool.getImport(cfg.entity.serviceItems)>
<#list imports as imp>
import ${imp}<#if !imp?ends_with(';')>;</#if>
</#list>

/**
 *
 * ${table.comment}服务自定义查询类
 *
 * @@tableName ${table.name}
 * @author ${author}
 */
@SuppressWarnings("all")
public interface Basic${table.entityName}Service extends IBasicService<${table.entityName}Query,${table.entityName},${table.entityName}Wrapper> {
<#if cfg.entity?? && cfg.entity.serviceItems??>
 <#list cfg.entity.serviceItems as service>

   /**
    * ${service.description}<#list service.items as param>
    * @param ${param.field.name}<#if param.filterType?? && service.resultType != 'UPDATE'>${param.filterType.serviceMethodName?cap_first}</#if> ${param.field.comment}</#list>
    */
   default <#switch service.resultType>
  <#case 'QUERY_ONE'>Optional<${table.entityName}> ${service.methodName}(<#list service.items as param><#assign paramJavaType=cfg.tool.getDbColumnType(param)>${paramJavaType}<#if param.filterType == 'IN' ||
  param.filterType == 'NOT_IN'>[] ${param.field.name}${param.filterType.serviceMethodName?cap_first}<#else > ${param.field.name}${param.filterType.serviceMethodName?cap_first}</#if><#if param_has_next>, </#if></#list>)<#break>
  <#case 'COUNT'>long ${service.methodName}(<#list service.items as param><#assign paramJavaType=cfg.tool.getDbColumnType(param)>${paramJavaType}<#if param.filterType == 'IN' ||
  param.filterType == 'NOT_IN'>[] ${param.field.name}${param.filterType.serviceMethodName?cap_first}<#else > ${param.field.name}${param.filterType.serviceMethodName?cap_first}</#if><#if param_has_next>, </#if></#list>)<#break>
  <#case 'QUERY_LIST'>List<${table.entityName}> ${service.methodName}(<#list service.items as param><#assign paramJavaType=cfg.tool.getDbColumnType(param)>${paramJavaType}<#if param.filterType == 'IN' ||
  param.filterType == 'NOT_IN'>[] ${param.field.name}${param.filterType.serviceMethodName?cap_first}<#else > ${param.field.name}${param.filterType.serviceMethodName?cap_first}</#if><#if param_has_next>, </#if></#list>)<#break>
  <#case 'UPDATE'>boolean ${service.methodName}(@NonNull ${table.entityName} ${table.entityName?uncap_first},<#list service.items as param><#assign paramJavaType=cfg.tool.getDbColumnType(param)>${paramJavaType} ${param.field.name}<#if param_has_next>, </#if></#list>)<#break>
  <#case 'ADD'>${table.entityName} ${service.methodName}(<#assign addByParams = cfg.tool.addServiceByParams(service.items)><#list addByParams as param><#assign paramJavaType=cfg.tool.getDbColumnType(param)>${paramJavaType} ${param.field.name}<#if param_has_next>, </#if></#list>)<#break>
 </#switch>{
      <#-- Assert校验 start -->
      <#assign assertSet=cfg.tool.assertChecks(service.resultType,service.items)>
      <#list assertSet as assert>
      ${assert}
      </#list>
      <#-- Assert校验 end -->
      <#if service.resultType == 'UPDATE'>
      <#-- update更新 start -->
      Assert.notNull(${table.entityName?uncap_first}.getId(),"必须提供主键ID");
      <#list service.items as param>
      ${table.entityName?uncap_first}.set${param.fieldName?cap_first}(safeValue(${param.field.name}, <#assign otherVal=cfg.tool.getOtherValue(param)>${otherVal}));
      </#list>
      return ${service.resultType.wrapperMethodName}(${table.entityName?uncap_first});
      <#-- update更新 end -->
      <#elseif service.resultType == 'ADD'>
      <#-- add新增 start -->
      ${table.entityName} ${table.entityName?uncap_first} = new ${table.entityName}();
      <#list service.items as param>
      <#if param.initValue??>
      <#assign className=cfg.tool.getClassSimpleName(param.field)>
      ${table.entityName?uncap_first}.set${param.fieldName?cap_first}(<#switch className
      ><#case 'String'><#if param.field.isEnum??>${table.entityName}.${param.fieldName?cap_first}.${param.initValue}<#else>"${param.initValue}"</#if><#break>
      <#default>${param.initValue}<#break>
      </#switch>);
      <#else>
      ${table.entityName?uncap_first}.set${param.fieldName?cap_first}(safeValue(${param.field.name}, <#assign otherVal=cfg.tool.getOtherValue(param)>${otherVal}));
      </#if>
      </#list>
      ${service.resultType.wrapperMethodName}(${table.entityName?uncap_first});
      return ${table.entityName?uncap_first};
      <#-- add新增 end -->
      <#else>
      <#-- select/count查询 start -->
      return <#if service.resultType == 'QUERY_ONE'>Optional.ofNullable(</#if>${service.resultType.wrapperMethodName}(${table.entityName}Query.builder().getWrapper()
              <#list service.items as param>
                  .${param.filterType.wrapperMethodName}(${table.entityName}::get${param.fieldName?cap_first},${param.field.name}${param.filterType.serviceMethodName?cap_first})<#if !param_has_next>)<#if service.resultType == 'QUERY_ONE'>)</#if>;</#if>
              </#list>
      <#-- select/count查询 end -->
      </#if>
   }
 </#list>
</#if>

}